/*
 * @Author: your name
 * @Date: 2021-05-19 16:03:11
 * @LastEditTime: 2021-05-27 19:03:07
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /xiangpan-admin/src/pages/chain/index.tsx
 */
import { IForm } from '@/components/form/interface';
import {
  Button,
  Card,
  Input,
  Modal,
  Space,
  Table,
  TablePaginationConfig,
} from 'antd';
import React, { useEffect, useState } from 'react';
import { PlusOutlined } from '@ant-design/icons';
import { useRequest } from 'umi';
import {
  delete_chain,
  get_admin_list,
  get_chains,
  get_chain_list,
} from '@/services/pages';
import useObj from '@/use/useObj';
import initPagination from '@/use/initPagination';
import moment from 'moment';
import { formatDateTime } from '@/utils/utils';
import AccountModal from './AccountModal';
import { IJudge, IS_Super } from '@/enum';
interface IProps {}

const Account = (props: IProps) => {
  const [obj, setObj] = useObj({
    ...initPagination,
    visible: false,
    refresh: null,
    row: null,
  });

  const { data, error, loading } = useRequest(
    () =>
      get_admin_list({
        page_size: obj.pagination.pageSize,
        page_num: obj.pagination.current,
      }),
    {
      refreshDeps: [obj.pagination, obj.refresh],
    },
  );

  useEffect(() => {
    setObj((obj) => {
      obj.pagination.total = data?.results[0]?.total || 0;
    });
  }, [data]);

  const deleteChain = (chain_id: number) => {
    Modal.confirm({
      title: '删除操作！',
      content: '你确定要删除该条数据？',
      onOk: async () => {
        await delete_chain({ chain_id: chain_id });
        setObj((obj) => {
          obj.pagination.current = 1;
          obj.refresh = Symbol();
        });
      },
    });
  };

  const columns: IForm[] = [
    {
      key: 'username',
      dataIndex: 'username',
      title: '账号',
    },
    {
      key: 'password',
      dataIndex: 'password',
      title: '密码',
    },
    {
      key: 'is_super',
      dataIndex: 'is_super',
      title: '账号身份',
      render: (text, record) => {
        return IS_Super[text];
      },
    },
    // {
    //   key: 'is_boss',
    //   dataIndex: 'is_boss',
    //   title: '是否是老板',
    //   render: (text, record) => {
    //     return IJudge[text];
    //   },
    // },
    {
      key: 'opt',
      dataIndex: 'opt',
      title: '操作',
      render(text, record) {
        return (
          <Space>
            <Button
              type="primary"
              danger
              onClick={() => deleteChain(record.chain_id)}
            >
              删除
            </Button>
            <Button
              type="primary"
              onClick={() => setObj({ row: record, visible: true })}
            >
              编辑
            </Button>
          </Space>
        );
      },
    },
  ];
  return (
    <Card title="账号管理">
      <Button
        type="primary"
        icon={<PlusOutlined />}
        style={{ marginBottom: 15 }}
        onClick={() => {
          setObj({ visible: true, row: null });
        }}
      >
        添加账号
      </Button>
      <Table
        rowKey={(record) => record.admin_id}
        loading={loading}
        columns={columns}
        dataSource={data?.results}
        pagination={obj.pagination}
        onChange={(pagination: TablePaginationConfig): void => {
          const { current, pageSize } = pagination;
          setObj((obj: any) => {
            obj.pagination.current = current;
            obj.pagination.pageSize = pageSize;
            obj.onReFresh = Symbol();
          });
        }}
      />
      {obj.visible && (
        <AccountModal
          visible={obj.visible}
          row={obj.row}
          close={() => setObj({ visible: false })}
          refresh={() => {
            setObj((obj) => {
              obj.pagination.current = 1;
              obj.refresh = Symbol();
            });
          }}
        />
      )}
    </Card>
  );
};
export default Account;
